#include <iostream>
#include <string>

#include "../comm/httplib.h"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_control;

int main()
{
    Server svr;
    Control ctrl;

    // 获取所有题目列表
    svr.Get("/all_questions", [&](const Request &req, Response &resp){
        std::string html;
        ctrl.AllQusetions(&html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 根据题目编号，获取题目内容
    svr.Get(R"(/question/(\d+))", [&](const Request &req, Response &resp)
    {
        std::string number = req.matches[1];
        std::string html;
        ctrl.OneQuestions(number, &html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 提交代码，判题
    svr.Post(R"(/judge/(\d+))", [&](const Request &req, Response &resp){
        std::string number = req.matches[1];
        std::string result_json;
        ctrl.Judge(number, req.body, &result_json);
        resp.set_content(result_json, "application/json;charset=utf-8");
        // std::string number = req.matches[1];
        // resp.set_content("你就当这是指定题目的判题: " + number, "text/plain; charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8888);

    return 0;
}